const Service = require('egg').Service;

class LiveService extends Service {
	async getLiveList() {
		const {
			ctx,
			app
		} = this;
		// 分页 模糊查询 
		const {
			pageIndex,
			pageSize,
			keyword,
			id
		} = ctx.query;
		let sql = `select * from live where 1=1 `
		let sqlCount = `select count(id) as total from live where 1=1 `
		if (id) {
			sql += `and id=${id} `
			sqlCount += `and id=${id} `
		}
		if (keyword) {
			sql += `and name like "%${keyword}%" `
			sqlCount += `and name like "%${keyword}%" `
		}
		if (pageIndex && pageSize) {
			sql += `limit ${(pageIndex-1)*pageSize},${pageSize}`
		}
		const data = await app.mysql.query(sql)
		const [{
			total
		}] = await app.mysql.query(sqlCount)
		return {
			data,
			total
		}
	}
	async delLiveList() {
		const {
			ctx,
			app
		} = this;
		const data = await app.mysql.delete('live', {
			id: ctx.params.id
		}); // 根据id删除
		return data
	}
	async editType() {
		const {
			ctx,
			app
		} = this
		const data = await app.mysql.update('live', {
			...ctx.request.body,
		})
		return data;
	}
	async detailList() {
		const {
			ctx,
			app
		} = this
		const {
			id
		} = ctx.params;
		const data = await app.mysql.get('live', {id});
		return data
	}
}

module.exports = LiveService;